package com.ml.servlet;

import com.ml.dao.DkDao;
import com.ml.dao.UserDao;
import com.ml.dao.impl.DkDaoImpl;
import com.ml.dao.impl.UserDaoImpl;
import com.ml.model.Dk;
import com.ml.util.ReadAccess;
import com.ml.util.UtilProperties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * @author zzy
 */
public class WorkHour extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        //PrintWriter out = res.getWriter();
        UserDao ud = new UserDaoImpl();
        DkDao dk = new DkDaoImpl();

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            String flag = request.getParameter("flag");

            //按照人查询
            String user = request.getParameter("user");
            String psd = request.getParameter("psd");
            Object sessionUser = request.getSession().getAttribute("user");

            if ((user == null || user.equals("")) && sessionUser == null) {
                request.setAttribute("msg", "用户名不能为空");
                request.getRequestDispatcher("/index.jsp").forward(request, response);
                return;
            } else {
                //放个sessiion
                if (sessionUser == null ||  user!=null ) {//第一次进入 //重新登陆的，但是没有退出session
                    if (!ud.isExistsUser(user)) {
                        //用户不存在
                        request.setAttribute("username", user);
                        request.setAttribute("msg", "用户不存在");
                        request.getRequestDispatcher("/index.jsp").forward(request, response);
                        return;
                    }else{//用户对了在给 session
                        request.getSession().setAttribute("user", user);
                    }
                } else {//第2次进入
                    user = sessionUser.toString();
                }

                String searchUser = request.getParameter("searchuser");//要查询的人的名字
                //从session中获取flag
                Object sessionFlag =  request.getSession().getAttribute("flag");
                if(sessionFlag!=null){
                    flag = sessionFlag.toString();
                }else{//没有的话认为是密码
                    flag = psd;
                }
                String flagAdmin = UtilProperties.getConfig("flag.admin");
                if(flag!=null && flag.equals(flagAdmin)){//有搜索权限的
                    //获取所有用户
                    if(sessionUser == null){//说明是第一次加入flag
                        List users = ud.getAllUser();
                        request.getSession().setAttribute("users", users);
                        request.getSession().setAttribute("flag", flag);
                    }
                    if(searchUser!=null && !searchUser.equals("")){
                        //searchUser =
                        user = searchUser ;//有搜索权限的
                    }
                }else{//没有搜索权限的，只能查看自己的
                    //user = searchUser ;
                }


                List<Dk> list = new ArrayList<>();
                String month = request.getParameter("month");
                String year = request.getParameter("year");
                Calendar date = Calendar.getInstance();//通过Calendar获取年月
                if (month == null || "".equals(month)) {//没有传递参数默认查询当月的
                    int m = date.get(Calendar.MONTH) + 1;
                    month = m+"";
                    //month = m+""; // m > 9 ? (m + "") : ("0" + m);
                }
                if(year==null || "".equals(year)){
                    year = String.valueOf(date.get(Calendar.YEAR));;
                }
                //开始查询
                //按照名字查询
                //结束时间现在默认查询为一个月的
                int m2 = Integer.valueOf(month) + 1;//下个月的
                String stratdate = year+"/"+ month ; // access 支持这种格式的时间 2019/6/01 06:59:59
                String enddate = year+"/"+  (Integer.valueOf(month)+1);
                list = dk.getDkRecord(stratdate, enddate, user);

                request.setAttribute("month", month);//设置默认查询为当月
                request.setAttribute("year", year);//设置默认查询为当年
                request.setAttribute("list", list);//最终的查询结果
                request.setAttribute("searchUser",searchUser);//查询的人
                request.getRequestDispatcher("/wk.jsp").forward(request, response);
                return;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //out.flush();
        //out.close();
        //response.sendRedirect("/ServletProject/fristServlet");//重定向
    }
}
